Next | Prev | Up | Top | Contents | Index

Searching for DSOs at Run Time

When you run a dynamically linked executable, the run-time linker, rld, identifies the DSOs required by the executable, loads the required DSOs, and if necessary relocates DSOs within the process's virtual address space, so that no two DSOs occupy the same location.The program header of a dynamically linked executable contains a field, the liblist, which lists the DSOs required by the executable.

When looking for a DSO, rld searches directories in the following sequence:

  1. the path of the DSO in the liblist (if an explicit path is given)

  2. RPATH if it's defined in the main executable

  3. LD_LIBRARY_PATH if defined

  4. the default path (/usr/lib:/lib)
RPATH is a colon-separated list of directories stored in the main executable. You can set RPATH by using the -rpath argument to ld:

ld -o myprog myprog.c -rpath /d/src/mylib libmylib.so -lc

This example links the program against libmylib.so in the current directory, and configures the executable such that rld searches the directory /d/src/mylib when searching for DSOs.

The LD_LIBRARY_PATH environment variable is a colon-separated list of directories to search for DSOs. This can be very useful for testing new versions of DSOs before installing them in their final location. You can set the environment variable _RLD_ROOT to a colon-separated list of directories. The run-time linker prepends these to the paths in RPATH and the paths in the default search path.

In all of the colon-separated directory lists, an empty field is interpreted as the current directory. A leading or trailing colon counts as an empty field. Thus, if you set LD_LIBRARY_PATH to:

/d/src/lib1:/d/src/lib2:

the run-time linker searches the directory /d/src/lib1, then the directory /d/src/lib2, and then the current directory.

Note: For security reasons, if an executable has its set-user-ID or set-group-ID bits set, the run-time linker ignores the environment variables LD_LIBRARY_PATH and _RLD_ROOT. However, it still searches the directories in RPATH and the default path.


Next | Prev | Up | Top | Contents | Index